package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Housing;
import com.etc.service.HousingService;
import com.etc.service.PersonService;
import com.etc.service.impl.HousingServiceImpl;
import com.etc.service.impl.PerviceServiceImpl;
import com.etc.util.PageData;

/**
 * Servlet implementation class indexServlet
 */
@WebServlet("/is.do")
public class indexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HousingService hService=new HousingServiceImpl();
	PersonService pService = new PerviceServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public indexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//1设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

//		
		String op="";
		if (request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		
		if (op.equals("")) {
			doQuery(request, response,"index.jsp");
		}
		else if (op.equals("detail")) {
			
			int housingId=Integer.valueOf(request.getParameter("housingId"));
			Housing thishs=hService.getHousingById(housingId);
			System.out.println(thishs);
			
			
			if(request.getSession().getAttribute("username")!=null) {
				String sess=request.getSession().getAttribute("username").toString();
				System.out.println("session : "+sess);

				//转发用户的用户昵称和头像使他能够被调用
				String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
				String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
				request.setAttribute("personImg",personImg );
				request.setAttribute("personNickName",personNickName );
			}
			
			request.setAttribute("thishs", thishs);
			doQuery(request, response,"detail.jsp");
		}
		else if (op.equals("search")) {
			
			
			doQuery(request, response,"search.jsp");
		}
		else if (op.equals("searchBypage")) {
			// 操作针对分页的操作
			// 三个参数 从页面来的
			int page = 1;
			int pageSize =6;
			String strLike = "";
			if (request.getParameter("page") != null) {
				page = Integer.valueOf(request.getParameter("page"));
			}
			
			//对page和pageSize进行区间的判断
			
			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.valueOf(request.getParameter("pageSize"));
			}
			if (request.getParameter("strLike") != null) {
				strLike = request.getParameter("strLike");
			}
			
			
			if(request.getSession().getAttribute("username")!=null) {
				String sess=request.getSession().getAttribute("username").toString();
				System.out.println("session : "+sess);

				//转发用户的用户昵称和头像使他能够被调用
				String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
				String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
				request.setAttribute("personImg",personImg );
				request.setAttribute("personNickName",personNickName );
			}
			
			
			
			PageData<Housing> pageData=hService.getHousingAll(page, pageSize, strLike);

			System.out.println("pagedata.size :"+pageData.getData());
			request.setAttribute("pageData", pageData);
			request.setAttribute("strLike", strLike);

		    request.getRequestDispatcher("search.jsp").forward(request, response);
			
		} 
		else if(op.equals("about")) {
			doQuery(request, response,"about.jsp");
		}
		else if(op.equals("aboutus")) {
			doQuery(request, response,"about_us.jsp");
		}
		else {
			doQuery(request, response,"index.jsp");
		}
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response,String url)
			throws ServletException, IOException {
		

		if(request.getSession().getAttribute("username")!=null) {
			String sess=request.getSession().getAttribute("username").toString();
			System.out.println("session : "+sess);

			//转发用户的用户昵称和头像使他能够被调用
			String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
			String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
			request.setAttribute("personImg",personImg );
			request.setAttribute("personNickName",personNickName );
		}

		
		
		
		
		//2调用service中的方法
		List<Housing> Housinglist=hService.SelHousingAll();
		//3传递数据给jsp可以使用内置对象request,对象原型是HttpServletRequest
		request.setAttribute("Housinglist", Housinglist);
		//4跳转到jsp页面去？ 转发
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
